package com.yystar.support.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;

/**
 * 
 * @author liuzhifei
 *
 */
public class HeaderCacheServletResponse extends HttpServletResponseWrapper {
    
    private int _status = SC_OK;
    
    private Map<String, String> _header = new HashMap<String, String>();

    public HeaderCacheServletResponse(HttpServletResponse response) {
        super(response);
    }
    
    @Override
    public void addDateHeader(String name, long date) {
        
        _header.put(name, String.valueOf(date));
        
        super.addDateHeader(name, date);
    }
    
    @Override
    public void addHeader(String name, String value) {

        _header.put(name, value);
        
        super.addHeader(name, value);
    }
    
    @Override
    public void addIntHeader(String name, int value) {

        _header.put(name, String.valueOf(value));
        
        super.addIntHeader(name, value);
    }
    
    @Override
    public void setHeader(String name, String value) {

        _header.put(name, value);
        
        super.setHeader(name, value);
    }
    
    @Override
    public void setDateHeader(String name, long date) {
        
        _header.put(name, String.valueOf(date));
        
        super.setDateHeader(name, date);
    }
    
    
    @Override
    public void setIntHeader(String name, int value) {

        _header.put(name, String.valueOf(value));
        
        super.setIntHeader(name, value);
    }
    
    
    public Map<String, String> getHeaders(){
        return _header;
    }
    
    public String getHeader(String name){
        return _header.get(name);
    }
    
    @Override
    public void setStatus(int sc) {
        _status = sc;
        
        super.setStatus(sc);
    }
    
    @Override
    public void sendError(int status) throws IOException {
        _status = status;

        super.sendError(status);
    }

    @Override
    public void sendError(int status, String msg) throws IOException {
        _status = status;

        super.sendError(status, msg);
    }
    
    @Override
    public void sendRedirect(String location) throws IOException {
        super.sendRedirect(location);

        setStatus(SC_MOVED_PERMANENTLY);
    }
    
    public int getStatus(){
        return _status;
    }

}
